package com.mba.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mba.activity.R;
import com.mba.activity.UserMainPageActivity;
import com.mba.bean.Comment;
import com.mba.bean.Message;
import com.mba.util.AsyncImageLoader;
import com.mba.util.AsyncImageLoader.ImageCallback;

public class CommentListAdapter extends BaseAdapter {
	//图片的异步下载类
    AsyncImageLoader asyncImageLoader;
    
	private Context context;
	private Message message;
	
	public CommentListAdapter(Context context, Message message) {
		this.context=context;
		this.message = message;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return message.getComments()!=null? message.getComments().size():0;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView==null){
			convertView= LayoutInflater.from(context).inflate(R.layout.home_list_item_reports_layout, null);
		}
		TextView date = (TextView) convertView.findViewById(R.id.home_list_item_report_date);
		TextView text = (TextView) convertView.findViewById(R.id.home_list_item_report_text);
		ImageView avater = (ImageView) convertView.findViewById(R.id.report_avater);
		TextView userName = (TextView) convertView.findViewById(R.id.home_list_item_user_name);
		
		avater.setOnClickListener( new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(),UserMainPageActivity.class);
				Activity activity = (Activity) v.getContext();
				activity.startActivity(intent);
			}
		});
		
		Comment comment = message.getComments().get(position);
		
		 asyncImageLoader = new AsyncImageLoader();
		//异步加载图片
		 Drawable avater_image = asyncImageLoader.loadDrawable(
               comment.getUser().getHeadPic(), avater,
               new ImageCallback() {
                   public void imageLoaded(Drawable drawable,
                           ImageView iv, String url) {
                       iv.setImageDrawable(drawable);
                   }
        });
		 
		if (avater_image != null) {
			avater.setImageDrawable(avater_image);	
		}
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		date.setText(sdf.format(comment.getCreateAt()));
		text.setText(comment.getText());
		userName.setText(comment.getUser().getUserName());
		userName.setTextSize(12.0f);
		
		return convertView;
	}

}
